package cn.enjoy.rabbitmq.handler.boot;

import cn.enjoy.rabbitmq.config.MqQueueConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * 注解方式绑定队列和交换机
 */
@Slf4j
@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = MqQueueConfig.QUEUE_ONE, durable = "true", exclusive = "false", autoDelete = "false"),
        key = MqQueueConfig.QUEUE_ONE,
        exchange = @Exchange(MqQueueConfig.EXCHANGE_DIRECT)
))
public class DriectQueueHandlerAnnotation {

    @RabbitHandler
    public void fanoutHandlerManualAck(Map msg, Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("direct 路由模式，direct.queue.1接收到的消息为：{}", msg.toString());
            int i = 10/0;
//            Thread.sleep(10000);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            throw e;
        }
    }

}
